﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormLogger.Communication;

namespace WinFormLogger.Logging
{
    public class Logger
    {
        private static Logger? instance ;
        private static readonly object lockObj = new object();
        /// <summary>
        /// 新日志事件
        /// </summary>
        public event EventHandler<LogEventArgs>? LogAdded;
        /// <summary>
        /// 日志事件处理器列表
        /// </summary>
        private static readonly List<IlogHandler> logHandlers = new List<IlogHandler>();
        /// <summary>
        /// 存储日志的列表
        /// </summary>
        private static readonly List<LogEventArgs> logs = new List<LogEventArgs>();


        public void AddHandler(IlogHandler handler)
        {
            if(handler !=null && !logHandlers.Contains(handler))
            {
                logHandlers.Add(handler);
            }
        }
        public IEnumerable<LogEventArgs> GetLogs()
        {
            lock (logs)
            {
                return new List<LogEventArgs>(logs);
            }
        }
        public void Log(string message, LogLevel level,string source)
        {
            if (level < LogLevel.Debug)
                return;
            var datetime = DateTime.Now;
            //string logMessage = $"{DateTime.Now}: [{level}] {message}";
            var args = new LogEventArgs(datetime,message, level,source);
            lock (logs) 
            {
                logs.Add(args);
                if(logs.Count > 100)
                {
                    logs.RemoveAt(0);
                }
            }
            //通知所有日志
            foreach(var handler in logHandlers)
            {
                handler.ProcessLog(args);
            }
            //触发日志事件
            LogAdded?.Invoke(this,args);
        }
        private Logger()
        {
            //私有构造函数，防止外部实例化
        }
        public static Logger Instance
        {
            get
            {
                //双重检查锁定单例模式
                if (instance == null)
                {
                    lock (lockObj)
                    {
                        if (instance == null) 
                        {
                            instance = new Logger();
                        }
                    }
                }
                return instance;
            }
        }
        public void Debug(string message,string? source = null)
        {
            Log(message, LogLevel.Debug,source);
        }

        public void Info(string message,string? source = null)
        {
            Log(message, LogLevel.Info,source);
        }

        public void Warning(string message, string? source = null)
        {
            Log(message, LogLevel.Warning,source);
        }

        public void Error(string message, string? source = null)
        {
            Log(message, LogLevel.Error,source);
        }

        public void Critical(string message, string? source = null)
        {
            Log(message, LogLevel.Critical,source);
        }
    }
    public class LogEventArgs: EventArgs
    {
        public DateTime DateTime { get; } 
        public string Message { get; }
        public LogLevel Level { get; }
        public string Source { get; }
        public LogEventArgs(DateTime dateTime,string message, LogLevel level,string? source = null)
        {
            DateTime = dateTime;
            Message = message;
            Level = level;
            Source = source;
        }
    }
    public interface IlogHandler
    {
        /// <summary>
        /// 处理日志
        /// </summary>
        /// <param name="logEvent"></param>
        void ProcessLog(LogEventArgs logEvent);
    }
}
